using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using SolanumSolstice_Windows.Units;
using SolanumSolstice_Windows.Mapping;


namespace SolanumSolstice_Windows.Buildings
{
    public class Beacon : Building
    {
        protected Civilian civArchetype;
        protected Subsister subArchetype;
        protected Survivalist survArchetype;

        protected Cop copArchetype;
        protected Soldier sldArchetype;
        protected SpecialOps soArchetype;

        protected Researcher rschArchetype;
        protected Scholar schArchetype;
        protected Genius genArchetype;


        protected SpawnChooser spawnChooser;
        protected bool spawnChooserIsVisible;

        protected bool subsistorIsLocked;
        protected bool survivalistIsLocked;
        protected bool soldierIsLocked;
        protected bool specOpIsLocked;
        protected bool scholarIsLocked;
        protected bool geniusIsLocked;

        protected bool isSpawning;
        protected TimeSpan timer;
        protected TimeSpan timerFinal;

        protected bool first;

        protected float m_spawnrate = 10; //in seconds
        //protected GameTime elapsed;
        protected double spawncountdown;

        #region Accessors

        public bool SubsistorIsLocked
        {
            get
            {
                return subsistorIsLocked;
            }
            set
            {
                subsistorIsLocked = value;
            }
        }

        public bool SurvivalistIsLocked
        {
            get
            {
                return survivalistIsLocked;
            }
            set
            {
                survivalistIsLocked = value;
            }
        }

        public bool SoldierIsLocked
        {
            get
            {
                return soldierIsLocked;
            }
            set
            {
                soldierIsLocked = value;
            }
        }

        public bool SpecOpIsLocked
        {
            get
            {
                return specOpIsLocked;
            }
            set
            {
                specOpIsLocked = value;
            }
        }

        public bool ScholarIsLocked
        {
            get
            {
                return scholarIsLocked;
            }
            set
            {
                scholarIsLocked = value;
            }
        }

        public bool GeniusIsLocked
        {
            get
            {
                return geniusIsLocked;
            }
            set
            {
                geniusIsLocked = value;
            }
        }

        public Civilian CivArchetype
        {
            get
            {
                return civArchetype;
            }
            set
            {
                civArchetype = value;
            }
        }

        public Subsister SubArchetype
        {
            get
            {
                return subArchetype;
            }
            set
            {
                subArchetype = value;
            }
        }

        public Survivalist SurvArchetype
        {
            get
            {
                return survArchetype;
            }
            set
            {
                survArchetype = value;
            }
        }

        public Cop CopArchetype
        {
            get
            {
                return copArchetype;
            }
            set
            {
                copArchetype = value;
            }
        }

        public Soldier SldArchetype
        {
            get
            {
                return sldArchetype;
            }
            set
            {
                sldArchetype = value;
            }
        }

        public SpecialOps SoArchetype
        {
            get
            {
                return soArchetype;
            }
            set
            {
                soArchetype = value;
            }
        }

        public Researcher RschArchetype
        {
            get
            {
                return rschArchetype;
            }
            set
            {
                rschArchetype = value;
            }
        }

        public Scholar SchArchetype
        {
            get
            {
                return schArchetype;
            }
            set
            {
                schArchetype = value;
            }
        }

        public Genius GenArchetype
        {
            get
            {
                return genArchetype;
            }
            set
            {
                genArchetype = value;
            }
        }

        public bool SpawnChooserIsVisible
        {
            get
            {
                return spawnChooserIsVisible;
            }
            set
            {
                spawnChooserIsVisible = value;
            }
        }

        public bool First
        {
            get
            {
                return first;
            }
            set
            {
                first = value;
            }
        }

        #endregion


        public Beacon(ContentManager content, string name, Vector2 position, Vector2 entrance, int zombieCount)
            : base(content, name, position, entrance, zombieCount)
        {
            civArchetype = new Civilian(content, position);
            subArchetype = new Subsister(content, position);
            survArchetype = new Survivalist(content, position);

            copArchetype = new Cop(content, position);
            sldArchetype = new Soldier(content, position);
            soArchetype = new SpecialOps(content, position);

            rschArchetype = new Researcher(content, position);
            schArchetype = new Scholar(content, position);
            genArchetype = new Genius(content, position);

            texture = content.Load<Texture2D>("Content\\Txtr\\Bldg\\building_beacon");
            width = texture.Width / 2;
            height = texture.Height;

            buildingType = BuildingType.Beacon;

            spawnChooser = new SpawnChooser(content, this);
            spawnChooserIsVisible = false;

            subsistorIsLocked = true;
            survivalistIsLocked = true;
            soldierIsLocked = true;
            specOpIsLocked = true;
            scholarIsLocked = true;
            geniusIsLocked = true;

            first = true;

            isSpawning = false;
            /* two ways to go about this:  
             * -The people are "invisible" until they reach the beacon
             * -The people are in the buildings and come out at random times and band together and go to the beacon.
             * 
             * civ's survival should go up dramatically if in a group.  
             * They should cluster together (3-4) and run toward the beacon, while avoiding zombies.
             * 
             * civilians should have zombie-avoidance built in....?
             * 
             * each new civilian's target should be the Beacon's location?
             * 
             */
            spawncountdown = 0;

        }

        public void SpawnCivilian()
        {
            isSpawning = true;
            timer = new TimeSpan(0);
            timerFinal = timer + new TimeSpan(0, 0, 5);
            if (!first)
            {
                SolanumSolstice.soundBank.PlayCue("beacon");
            }
            Unit unit = (Unit)civArchetype.Clone();
            Building b = getEmptyHouse();
            unit.Position = b.Entrance;
            unit.SetTarget(this.Entrance);
            SolanumSolstice.activeUnits.Add((Human)unit);
        }

        public void SpawnSubsister()
        {
            isSpawning = true;
            timer = new TimeSpan(0);
            timerFinal = timer + new TimeSpan(0, 0, 5);
            if (!first)
            {
                SolanumSolstice.soundBank.PlayCue("beacon");
            }
            Unit unit = (Unit)subArchetype.Clone();
            Building b = getEmptyHouse();
            unit.Position = b.Entrance;
            unit.SetTarget(this.Entrance);
            SolanumSolstice.activeUnits.Add((Human)unit);
        }

        public void SpawnSurvivalist()
        {
            isSpawning = true;
            timer = new TimeSpan(0);
            timerFinal = timer + new TimeSpan(0, 0, 5);
            if (!first)
            {
                SolanumSolstice.soundBank.PlayCue("beacon");
            }
            Unit unit = (Unit)survArchetype.Clone();
            Building b = getEmptyHouse();
            unit.Position = b.Entrance;
            unit.SetTarget(this.Entrance);
            SolanumSolstice.activeUnits.Add((Human)unit);
        }

        public void SpawnResearcher()
        {
            isSpawning = true;
            timer = new TimeSpan(0);
            timerFinal = timer + new TimeSpan(0, 0, 5);
            if (!first)
            {
                SolanumSolstice.soundBank.PlayCue("beacon");
            }
            Unit unit = (Unit)rschArchetype.Clone();
            Building b = SolanumSolstice.scibase;
            unit.Position = b.Entrance;
            unit.SetTarget(this.Entrance);
            SolanumSolstice.activeUnits.Add((Human)unit);
        }

        public void SpawnScholar()
        {
            isSpawning = true;
            timer = new TimeSpan(0);
            timerFinal = timer + new TimeSpan(0, 0, 5);
            if (!first)
            {
                SolanumSolstice.soundBank.PlayCue("beacon");
            }
            Unit unit = (Unit)schArchetype.Clone();
            Building b = SolanumSolstice.scibase;
            unit.Position = b.Entrance;
            unit.SetTarget(this.Entrance);
            SolanumSolstice.activeUnits.Add((Human)unit);
        }

        public void SpawnGenius()
        {
            isSpawning = true;
            timer = new TimeSpan(0);
            timerFinal = timer + new TimeSpan(0, 0, 5);
            if (!first)
            {
                SolanumSolstice.soundBank.PlayCue("beacon");
            }
            Unit unit = (Unit)genArchetype.Clone();
            Building b = SolanumSolstice.scibase;
            unit.Position = b.Entrance;
            unit.SetTarget(this.Entrance);
            SolanumSolstice.activeUnits.Add((Human)unit);
        }

        public void SpawnCop()
        {
            isSpawning = true;
            timer = new TimeSpan(0);
            timerFinal = timer + new TimeSpan(0, 0, 5);
            if (!first)
            {
                SolanumSolstice.soundBank.PlayCue("beacon");
            }
            Unit unit = (Unit)copArchetype.Clone();
            Building b = SolanumSolstice.milbase;
            unit.Position = b.Entrance;
            unit.SetTarget(this.Entrance);
            SolanumSolstice.activeUnits.Add((Human)unit);
        }

        public void SpawnSoldier()
        {
            isSpawning = true;
            timer = new TimeSpan(0);
            timerFinal = timer + new TimeSpan(0, 0, 5);
            if (!first)
            {
                SolanumSolstice.soundBank.PlayCue("beacon");
            }
            Unit unit = (Unit)sldArchetype.Clone();
            Building b = SolanumSolstice.milbase;
            unit.Position = b.Entrance;
            unit.SetTarget(this.Entrance);
            SolanumSolstice.activeUnits.Add((Human)unit);
        }

        public void SpawnSpecialOps()
        {
            isSpawning = true;
            timer = new TimeSpan(0);
            timerFinal = timer + new TimeSpan(0, 0, 5);
            if (!first)
            {
                SolanumSolstice.soundBank.PlayCue("beacon");
            }
            Unit unit = (Unit)soArchetype.Clone();
            Building b = SolanumSolstice.milbase;
            unit.Position = b.Entrance;
            unit.SetTarget(this.Entrance);
            SolanumSolstice.activeUnits.Add((Human)unit);
        }



        public override void Update(GameTime gameTime, GamePadState gamePad, GameCursor gameCursor)
        {
            base.Update(gameTime, gamePad, gameCursor);
            /*
            TimeSpan elapsed = gameTime.ElapsedGameTime;

            spawncountdown -= elapsed.Milliseconds / 1000.0f;

            if (spawncountdown < 0)
            {
                //spawn a new civvie
                SpawnCivilian();
                SpawnSubsister();
                SpawnGenius();
                
                SpawnCop();
                SpawnSoldier();
                SpawnSpecialOps();

                SpawnResearcher();
                SpawnScholar();
                SpawnGenius();
      
                //countdown
                spawncountdown = m_spawnrate;
            }
             */
            if (isSpawning)
            {
                timer += gameTime.ElapsedRealTime;

                if (timer.TotalMilliseconds > timerFinal.TotalMilliseconds)
                {
                    isSpawning = false;
                }
            }
            //SpawnChooser stuff
            if (gamePad.Triggers.Left > 0)
            {
                spawnChooserIsVisible = true;
                spawnChooser.Update(gameTime, gamePad, gameCursor);

            }
            else
            {
                spawnChooserIsVisible = false;
            }
        }


        public override void DrawHUD(SpriteBatch spritebatch, Rectangle drawBoundary)
        {
            base.DrawHUD(spritebatch, drawBoundary);

            if (spawnChooserIsVisible == true)
            {
                spawnChooser.Draw(spritebatch);
            }
        }

        public override void Draw(SpriteBatch spritebatch, int mapFocusX, int mapFocusY, GameTime gameTime)
        {
            if (isSpawning == true)
            {
                spritebatch.Draw(texture, new Rectangle((int)position.X - mapFocusX, (int)position.Y - mapFocusY, width, height), new Rectangle(96, 0, 96, 96), Color.White);
            }
            else
            {
                spritebatch.Draw(texture, new Rectangle((int)position.X - mapFocusX, (int)position.Y - mapFocusY, width, height), new Rectangle(0, 0, 96, 96), Color.White);
            }

            if (buildingType == BuildingType.Beacon || buildingType == BuildingType.Supply || buildingType == BuildingType.Research)
            {
                infestedYincrement += 4.5f;


                infestedYOffset = (float)Math.Sin(MathHelper.ToRadians(infestedYincrement)) * 4;



                spritebatch.Draw(greenArrow, new Rectangle((int)position.X - mapFocusX + (width / 2) - 25, (int)position.Y - mapFocusY + (int)infestedYOffset, 35, 35), Color.White);

            }
        }

        Building getEmptyHouse()
        {

            int index = rand.Next(SolanumSolstice.houseList.Count);
            while (SolanumSolstice.houseList[index].IsInfested)
            {
                index = rand.Next(SolanumSolstice.houseList.Count);
            }
            return SolanumSolstice.houseList[index];
        }



    }
}

